<?php defined('SYSPATH') or die('No direct script access.');

class Menu {

    public static function print_menu($parent = 0, $depth = -1) {
        $menus = ORM::factory('menu')
                ->where('parent_id', '=', $parent)
                ->order_by('pos')
                ->find_all();
        
        if ($menus->count() < 1)
            return;

        echo "<ul>";
        
        foreach ($menus as $menu) {
            $uri = $menu->uri;
            $attributes = NULL;
            
            if (strpos($uri, 'http://') !== FALSE) {
                $attributes = array('target' => '_blank');
            } elseif ($uri != "#") {
                $uri = Controller_App::getLanguage()
                        . '/'
                        . trim($uri, '/')
                        . '/';
            }
            
            echo "<li>";
            echo Html::anchor($uri, __($menu->title), $attributes);
            
            if ($depth != 0)
                self::print_menu($menu->id, $depth - 1);
            
            echo "</li>";
        }
        
        echo "</ul>";
    }

}
